﻿using FtpFileBackup_Oracle.LogHelper;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace FtpFileBackup_Oracle.Client
{
    class Program
    {
        private static string DownFtpPath => ConfigurationManager.AppSettings["downFtpPath"];
        private static string DownFtpUserID => ConfigurationManager.AppSettings["downFtpUserID"];
        private static string DownFtpPassword => ConfigurationManager.AppSettings["downFtpPassword"];
        private static string DwonPath => ConfigurationManager.AppSettings["downPath"];
        private static string UpFtpPath => ConfigurationManager.AppSettings["upFtpPath"];
        private static string UpFtpUserID => ConfigurationManager.AppSettings["upFtpUserID"];
        private static string UpFtpPassword => ConfigurationManager.AppSettings["upFtpPassword"];
        static void Main(string[] args)
        {
            FileBackup();
        }

        public static void FileBackup()
        {
            Action taskDownload = () =>
            {
                log4netHelper.Info("开始下载备份");

                var backupDir = DwonPath + "\\";
                if (!Directory.Exists(backupDir))
                {
                    Directory.CreateDirectory(backupDir);
                }

                var success = FtpHelper.DownloadFtp(DwonPath, $"{DateTime.Now.ToString("yyyyMMddHH0000")}.7z", DownFtpPath, DownFtpUserID, DownFtpPassword);

                if (success)
                {
                    log4netHelper.Info("完成下载备份");
                }
                else
                {
                    throw new Exception("下载备份失败，请查看日志");
                }
            };

            Action<Task> taskUpload = (antecendent) =>
            {
                log4netHelper.Info("开始上传备份");

                var success = FtpHelper.UploadFtp(DwonPath, $"{DateTime.Now.ToString("yyyyMMddHH0000")}.7z", $"{UpFtpUserID}@{UpFtpPath}", UpFtpUserID, UpFtpPassword);

                if (success)
                {
                    log4netHelper.Info("完成上传备份");
                }
                else
                {
                    throw new Exception("上传备份失败，请查看日志");
                }
            };

            Action<Task> taskDelOld = (antecendent) =>
            {
                log4netHelper.Info("开始删除过期备份");

                var date = DateTime.Now.AddDays(-2);

                var success = FtpHelper.DeleteFtp($"{date.ToString("yyyyMMddHH0000")}.7z", $"{DownFtpUserID}@{DownFtpPath}", DownFtpUserID, DownFtpPassword);

                if (success)
                {
                    log4netHelper.Info("完成删除过期备份");
                }
                else
                {
                    throw new Exception("删除过期备份失败，请查看日志");
                }
            };

            try
            {
                Task.Factory.StartNew(taskDownload)
                    .ContinueWith(taskUpload)
                    .ContinueWith(taskDelOld)
                    .Wait();
            }
            catch (AggregateException e)
            {
                var errMessage = e.InnerExceptions.Aggregate(string.Empty, (cur, next) => { return cur == string.Empty ? next.Message : cur + ";" + next.Message; });

                log4netHelper.Error(errMessage);
            }
        }
    }
}
